package com.whq.thrift.api.web.admin.controller.bs;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;

import redis.clients.jedis.Jedis;

/**
 * redis 测试工具
 * 
 * @author whq-mac
 * 
 */

@Controller
@Scope("prototype")
public class RedisTestController {
	private static final Logger LOGGER = LoggerFactory.getLogger(RedisTestController.class);

	@RequestMapping(value = "bs/redistest/index")
	public String index(@RequestParam(value = "ipAddress", required = false) String ipAddress, @RequestParam(value = "port", required = false) Integer port,
			@RequestParam(value = "pwd", required = false) String pwd, @RequestParam(value = "key", required = false) String key, Model model,
			HttpServletRequest request) {
		model.addAttribute("ipAddress", ipAddress);
		model.addAttribute("port", port);
		model.addAttribute("pwd", pwd);
		model.addAttribute("key", key);

		String val = "";
		if (StringUtil.isBlank(ipAddress) || (port == null) || StringUtil.isBlank(key)) {
			model.addAttribute("val", val);
			return "bs/redistest/index";
		}

		// 连接redis服务
		Jedis jedis = new Jedis(ipAddress, port);

		// 密码验证-如果你没有设置redis密码可不验证即可使用相关命令
		if (StringUtil.isNotBlank(pwd)) {
			jedis.auth(pwd);
		}

		// 简单的key-value 存储
		// jedis.set("redis", "myredis");

		val = jedis.get(key);

		model.addAttribute("val", val);

		jedis.disconnect();

		return "bs/redistest/index";
	}
}